package com.lbn.monitor.widget.chart;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.lbn.monitor.widget.AnimListener;
import com.lbn.monitor.widget.calendar.TimeUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 李炳南
 * @date 2017/3/7
 */
public class ChartContent extends View {

    private int viewHeight, viewWidth;
    private int textSize;
    private int itemHeight, itemWidth;
    private Paint paint;
    private TextPaint textPaint;
    private final int DATA_VALUE_MAX = 4;
    private List<Element> data;
    private double maxValue = 1000;

    public ChartContent(Context context) {
        this(context, null);
    }

    public ChartContent(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChartContent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        data = new ArrayList<>();
        setData();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        viewHeight = MeasureSpec.getSize(heightMeasureSpec);
        viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        viewHeight = viewHeight - textSize * 5;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBottomText(canvas);
        drawPoint(canvas);
        drawPointText(canvas);
    }


    public void drawPoint(Canvas canvas) {
        for (int i = 0; i < data.size(); i++) {
            Element element = data.get(i);
            double y = (1 - element.getY() / maxValue) * itemHeight * 10 + (viewHeight - itemHeight * 10);
            element.setPointY((int) y);
            canvas.drawCircle(element.getPointX(), element.getPointY(), textSize / 2, paint);
        }

        int size = data.size();
        for (int i = 0; i < size; i++) {
            if (i == size - 1) {
                return;
            }
            Point point = data.get(i).getPoint();
            Point pointP = data.get(i + 1).getPoint();
            canvas.drawLine(point.x, point.y, pointP.x, pointP.y, paint);
        }
    }

    public void drawPointText(Canvas canvas) {

        for (int i = 0; i < data.size(); i++) {
            Element element = data.get(i);
            String text = String.valueOf(element.getY());
            float mSize = textPaint.measureText(text);
            canvas.drawText(text,element.getPointX()-mSize/2,element.getPointY() - textSize/2,textPaint);
        }

    }


    public void drawBottomText(Canvas canvas) {

        textPaint.setTextSize(textSize);
        for (int i = 0; i < data.size(); i++) {
//            double y = (1 - data.get(i).getY()/MaxValue)*viewHeight;
            String date = TimeUtils.milliseconds2String(data.get(i).getX());
            float dateSize = textPaint.measureText(date);
            canvas.drawText(date, -dateSize + itemWidth * (i + 1), viewHeight + textSize, textPaint);
            float startX = -dateSize / 2 + itemWidth * (i + 1);
            data.get(i).setPointX((int) startX);
            canvas.drawLine(startX, viewHeight, startX, viewHeight - textSize, paint);
        }
        canvas.drawLine(0, viewHeight, viewWidth * 2, viewHeight, paint);
    }

    public void setTestSize(int size) {
        textSize = size;
    }


    public void setPaint(Paint paint) {
        this.paint = paint;
    }

    public void setItemSize(int width, int height) {
        itemWidth = width;
        itemHeight = height;
    }

    public void setTextPaint(TextPaint textPaint) {
        this.textPaint = textPaint;
    }

    public void addElement(Element element) {
        data.add(element);
        invalidate();
        startAnimation();
    }


    public void setMaxValue(double maxValue) {
        this.maxValue = maxValue;
    }

    public void setData() {
//        data.add(new Element(1488880132,300));
//        data.add(new Element(1488980132,250));
//        data.add(new Element(1489070132,700));
//        data.add(new Element(1489170132,600));
//        data.add(new Element(1489270132,400));
    }

    int count = 0;

    public void startAnimation() {

        if (data.size() < DATA_VALUE_MAX)
            return;
//        int moveValue = 0;
//        if(count>0){
//            moveValue = itemWidth*2;
//        }else{
//            count ++;
//            moveValue = itemWidth;
//        }

        ValueAnimator animator = ValueAnimator.ofInt(0, itemWidth);
        animator.setDuration(500);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                scrollTo(value, 0);
            }
        });
//        final int finalMoveValue = moveValue;
        animator.addListener(new AnimListener() {
            @Override
            public void onAnimationEnd(Animator animator) {
                super.onAnimationEnd(animator);
                try {
                    data.remove(0);
                }catch (Exception e){

                }
                scrollTo(0, 0);
                invalidate();
            }
        });
        animator.start();
    }

    public void clear(){
        maxValue = 1000;
        data.clear();
    }
}
